################################################################
# Replication file for: "Does Process Matter? Direct Democracy #
# and Citizens' Perceptions of Laws"                           #
#                                                              #
# Supplementary Information                                    #
# Significance Tests for Tables 1 & 2                          #
# Figures 1-8                                                  #
#                                                              #
# Christina Ladam                                              #
# University of Colorado Boulder                               #
# christina.ladam@colorado.edu                                 #
#                                                              #
# Last updated: May 22, 2019                                   #
################################################################

### Packages ###
library(foreign)
library(ggplot2)
library(extrafont)
library(cowplot)
library(visreg)
library(xtable)
library(plyr)

### Data preparation ###
study1<-read.csv("study1.csv")
study2<-read.csv("study2.csv")

# Create labels for ballot treatment variables
study1$ballot<-factor(study1$ballot, levels = c(0,1), labels = c("State Legislature", "Ballot Initiative"))
study2$ballot<-factor(study2$ballot, levels = c(0,1), labels = c("State Legislature", "Ballot Initiative"))

### TABLES ###

### Table 1 ###

## Study 1: Same-sex Marriage ##
# Subset to Same-sex Treatment
study1gm<-subset(study1, t1 == 1 | t2 == 1)

# T-test for Process Fairness (Same-sex Marriage)
gm.fair<-t.test(fairpassage ~ ballot, data=study1gm, var.equal=T)
gm.fair.diff<-gm.fair$estimate[2]-gm.fair$estimate[1]
gm.fair.diff<-rename(gm.fair.diff, replace=c("mean in group Ballot Initiative"="Mean diff."))
gm.fair.se<-abs(gm.fair.diff/gm.fair$statistic)
gm.fair.se<-rename(gm.fair.se, replace=c("Mean diff."="Std. Error"))
gm.fair.p<-gm.fair$p.value
names(gm.fair.p)<-"Sig."
gm.fair.results<-c(gm.fair.diff, gm.fair.se, gm.fair$parameter, gm.fair$statistic, gm.fair.p)

# T-test for Support for law (Same-sex Marriage)
gm.support<-t.test(supportlaw ~ ballot, data=study1gm, var.equal=T)
gm.support.diff<-gm.support$estimate[2]-gm.support$estimate[1]
gm.support.se<-abs(gm.support.diff/gm.support$statistic)
gm.support.results<-c(gm.support.diff, gm.support.se, gm.support$parameter, gm.support$statistic, gm.support$p.value)
gm.support.results<-unname(gm.support.results)

### Study 1: Personnel Guidelines ###

# Subset to Personnel Guidelines Treatment
study1pg<-subset(study1, t3 == 1 | t4 == 1)

# T-test for Process Fairness (Personnel Guidelines)
pg.fair<-t.test(fairpassage ~ ballot, data=study1pg, var.equal=T)
pg.fair.diff<-pg.fair$estimate[2]-pg.fair$estimate[1]
pg.fair.se<-abs(pg.fair.diff/pg.fair$statistic)
pg.fair.results<-c(pg.fair.diff, pg.fair.se, pg.fair$parameter, pg.fair$statistic, pg.fair$p.value)
pg.fair.results<-unname(pg.fair.results)

# T-test for Support for law (Personnel Guidelines)
pg.support<-t.test(supportlaw ~ ballot, data=study1pg, var.equal=T)
pg.support.diff<-pg.support$estimate[2]-pg.support$estimate[1]
pg.support.se<-abs(pg.support.diff/pg.support$statistic)
pg.support.results<-c(pg.support.diff, pg.support.se, pg.support$parameter, pg.support$statistic, pg.support$p.value)
pg.support.results<-unname(pg.support.results)

### TABLE 1 LATEX CODE ###
xtable(rbind(gm.fair.results, pg.fair.results, gm.support.results, pg.support.results), hline.after=c(-1,1,1))

### TABLE 2 ###

## Study 2: Marijuana Decriminalization ##

# Subset to Marijuana Decriminalization Treatment
study2md<-subset(study2, t1 == 1 | t2 == 1)

# T-test for Process Fairness (Marijuana Decriminalization)
md.fair<-t.test(fairpassage ~ ballot, data=study2md, var.equal=T)
md.fair.diff<-md.fair$estimate[2]-md.fair$estimate[1]
md.fair.diff<-rename(md.fair.diff, replace=c("mean in group Ballot Initiative"="Mean diff."))
md.fair.se<-abs(md.fair.diff/md.fair$statistic)
md.fair.se<-rename(md.fair.se, replace=c("Mean diff."="Std. Error"))
md.fair.p<-md.fair$p.value
names(md.fair.p)<-"Sig."
md.fair.results<-c(md.fair.diff, md.fair.se, md.fair$parameter, md.fair$statistic, md.fair.p)

# T-test for Support for law (Marijuana Decriminalization)
md.support<-t.test(supportlaw ~ ballot, data=study2md, var.equal=T)
md.support.diff<-md.support$estimate[2]-md.support$estimate[1]
md.support.se<-abs(md.support.diff/md.support$statistic)
md.support.results<-c(md.support.diff, md.support.se, md.support$parameter, md.support$statistic, md.support$p.value)
md.support.results<-unname(md.support.results)

### Study 2: Auto Insurance ###

# Subset to Auto Insurance Treatment
study2ai<-subset(study2, t3 == 1 | t4 == 1)

# T-test for Process Fairness (Auto Insurance)
ai.fair<-t.test(fairpassage ~ ballot, data=study2ai, var.equal=T)
ai.fair.diff<-ai.fair$estimate[2]-ai.fair$estimate[1]
ai.fair.se<-abs(ai.fair.diff/ai.fair$statistic)
ai.fair.results<-c(ai.fair.diff, ai.fair.se, ai.fair$parameter, ai.fair$statistic, ai.fair$p.value)
ai.fair.results<-unname(ai.fair.results)

# T-test for Support for law (Auto Insurance)
ai.support<-t.test(supportlaw ~ ballot, data=study2ai, var.equal=T)
ai.support.diff<-ai.support$estimate[2]-ai.support$estimate[1]
ai.support.se<-abs(ai.support.diff/ai.support$statistic)
ai.support.results<-c(ai.support.diff, ai.support.se, ai.support$parameter, ai.support$statistic, ai.support$p.value)
ai.support.results<-unname(ai.support.results)

### TABLE 2 LATEX CODE ###
xtable(rbind(md.fair.results, ai.fair.results, md.support.results, ai.support.results), hline.after=c(-1,1,1))



### FIGURES ###

### Figure 1 ###
# Model 
m.gm.interaction<-glm(supportlaw~ballot*gayrights, data=study1gm, family=binomial(link="logit"))
# Figure
pdf(file="figure1.pdf", width=6.5, height=5)
visreg(m.gm.interaction, "gayrights", by="ballot", overlay=TRUE, scale="response", xlab="1=Strongly Disagree ... 7=Strongly Agree", ylab="P(Support Law)", legend=T)
dev.off()


### Figure 2 ###
# Model
m.pg.interaction<-glm(supportlaw~ballot*merit, data=study1pg, family=binomial(link="logit"))
# Figure
pdf(file="figure2.pdf", width=6.5, height=5)
visreg(m.pg.interaction, "merit", by="ballot", overlay=TRUE, scale="response", xlab="1=Strongly Disagree ... 7=Strongly Agree", ylab="P(Support Law)", legend=T)
dev.off()


### Figure 3 ###
# Model
m.md.interaction<-glm(supportlaw~ballot*criminalization, data=study2md, family=binomial(link="logit"))
# Figure
pdf(file="figure3.pdf", width=6.5, height=5)
visreg(m.md.interaction, "criminalization", by="ballot", overlay=TRUE, scale="response", xlab="1=Strongly Disagree ... 7=Strongly Agree", ylab="P(Support Law)", legend=T)
dev.off()

### Figure 4 ###
# Model
m.ai.interaction<-glm(supportlaw~ballot*regulation, data=study2ai, family=binomial(link="logit"))
# Figure
pdf(file="figure4.pdf", width=6.5, height=5)
visreg(m.ai.interaction, "regulation", by="ballot", overlay=TRUE, scale="response", xlab="1=Strongly Disagree ... 7=Strongly Agree", ylab="P(Support Law)", legend=T)
dev.off()


### Figure 5 ###
# Model
m.gm.interaction2<-lm(fairpassage~ballot*gayrights, data=study1gm)
# Figure
pdf(file="figure5.pdf", width=6.5, height=5)
visreg(m.gm.interaction2, "gayrights", by="ballot", overlay=TRUE, scale="response", xlab="1=Strongly Disagree ... 7=Strongly Agree", ylab="Perceived Fairness", legend=T)
dev.off()

### Figure 6 ###
# Model
m.pg.interaction2<-lm(fairpassage~ballot*merit, data=study1pg)
# Figure
pdf(file="figure6.pdf", width=6.5, height=5)
visreg(m.pg.interaction2, "merit", by="ballot", overlay=TRUE, scale="response", xlab="1=Strongly Disagree ... 7=Strongly Agree", ylab="Perceived Fairness", legend=T)
dev.off()


### Figure 7 ###
# Model
m.md.interaction2<-lm(fairpassage~ballot*criminalization, data=study2md)
# Figure
pdf(file="figure7.pdf", width=6.5, height=5)
visreg(m.md.interaction2, "criminalization", by="ballot", overlay=TRUE, scale="response", xlab="1=Strongly Disagree ... 7=Strongly Agree", ylab="Perceived Fairness", legend=T)
dev.off()

### Figure 8 ###
# Model
m.ai.interaction2<-lm(fairpassage~ballot*regulation, data=study2ai)
# Figure
pdf(file="figure8.pdf", width=6.5, height=5)
visreg(m.ai.interaction2, "regulation", by="ballot", overlay=TRUE, scale="response", xlab="1=Strongly Disagree ... 7=Strongly Agree", ylab="Perceived Fairness", legend=T)
dev.off()

